// pages/community/addGoods/addGoods.js
const storeService = require('../../../../api/storeService.js')
const productService = require('../../../../api/productService.js')
const util = require('../../../../utils/utils.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchLabelShow: false,
    searchFocus: false,
    searchItem: '',
    searchText: '搜索你要添加的商品',
    nameLikes: '',
    keyboardHeight: 0,
    goodsList: [],
    store: '',
    storeId: '',
    page: 1,
    pageSize: 0,
  },
  // 搜索框点击事件,输入框聚焦
  handleFocus: function (e) {
    this.setData({
      searchLabelShow: true,
      goodsList: [],
    })
  },
  // 搜索框聚焦事件
  handleInputFocus: function (e) {
    console.log(e.detail)
    this.setData({
      keyboardHeight: e.detail.height
    })
  },
  // 搜索框失去焦点处理
  handleBlur: function (e) {
    if (!e.detail.value) {
      this.setData({
        searchLabelShow: false,
        goodsList: [],
        keyboardHeight: 0
      })
    } else {
      this.setData({
        keyboardHeight: 0
      })
    }
  },
  // 输入框输入时处理
  handleInput: function (e) {
    this.setData({
      nameLikes: e.detail.value
    })
  },
  // 搜索框确认事件
  handleConfirm: function (e) {
    console.log(e)
    if (!e.detail.value) {
      this.setData({
        searchLabelShow: false,
        keyboardHeight: 0
      })
      wx.showToast({
        title: '请输入您想搜索的商品名称！',
        icon: "none",
        duration: 2000
      })
    } else {
      var nameLikes = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
      this.getSearchProduct(nameLikes)
    }
  },
  // 搜索商品
  getSearchProduct: function (nameLikes) {
    var storeId = this.data.storeId;
    const that = this
    var page = this.data.page;
    var pageSize = this.data.pageSize;
    var searchCount = true;
    console.log(nameLikes)
    productService.getSearchProduct(storeId, page, pageSize, nameLikes, searchCount).then(res => {
      console.log(res);
      let tempList = []
      if (res.records && res.records.length > 0) {
        res.records.forEach(item => {
          let produtlabel = []
          let promotions = []
          let specifications = ''
          if (item.labels && item.labels.length > 0) {
            item.labels.forEach(ele => {
              if (ele.type === 'GOODSLABEL') {
                produtlabel.push(ele)
              } else if (ele.type === 'PROMOTIONLABEL') {
                promotions.push(ele)
              }
            })
          }
          if (item.productProperties && item.productProperties.length > 0) {
            for (let i = 0; i < item.productProperties.length; i++) {
              const ele = item.productProperties[i];
              if (ele.propertyName === '规格') {
                specifications = ele.propertyValue
                break
              }
            }
          }
          let tempItem = {
            ...item,
            produtlabel: produtlabel,
            promotions: promotions,
            specifications: specifications
          }
          tempList.push(tempItem)
        })
        that.setData({
          goodsList: tempList
        })
      } else {
        wx.showToast({
          title: '没有您想搜索的商品！',
          icon: "none",
          duration: 2000
        })
      }
    })
  },
  // 获取门店精选商品
  getStoreProduct: function (storeId) {
    const that = this
    let page = 1
    let pageSize = 0
    let business = 'RETAIL'
    let hot = true
    let postData = {
      page: page,
      pageSize: pageSize,
      hot: hot,
      storeIdEquals: storeId,
      business: business
    }
    productService.query(postData).then(res => {
      console.log(res)
      let tempList = []
      if (res.records && res.records.length > 0) {
        res.records.forEach(item => {
          let produtlabel = []
          let promotions = []
          let specifications = ''
          if (item.labels && item.labels.length > 0) {
            item.labels.forEach(ele => {
              if (ele.type === 'GOODSLABEL') {
                produtlabel.push(ele)
              } else if (ele.type === 'PROMOTIONLABEL') {
                promotions.push(ele)
              }
            })
          }
          if (item.productProperties && item.productProperties.length > 0) {
            for (let i = 0; i < item.productProperties.length; i++) {
              const ele = item.productProperties[i];
              if (ele.propertyName === '规格') {
                specifications = ele.propertyValue
                break
              }
            }
          }
          let tempItem = {
            ...item,
            produtlabel: produtlabel,
            promotions: promotions,
            specifications: specifications
          }
          tempList.push(tempItem)
        })
        let newTempList = that.productsSort(tempList)
        that.setData({
          goodsList: newTempList
        })
      } else {
        that.setData({
          goodsList: tempList
        })
      }
    })
  },
  productsSort: function (array) {
    let balanceArray = []
    let allArray = []
    array.forEach(ele => {
      if (ele.balance === 0) {
        balanceArray.push(ele)
      } else {
        allArray.push(ele)
      }
    });
    const tempArray = allArray.concat(balanceArray)
    return tempArray
  },
  addGoods: function (val) {
    const that = this
    const goodId = val.currentTarget.dataset.goodid
    const goodsList = that.data.goodsList
    let goodItem = {}
    goodsList.forEach(item => {
      if (item.id === goodId) {
        goodItem = item
      }
    })
    try {
      wx.setStorageSync('wj_tempGoods', goodItem)
    } catch (e) {

    }
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const self = this;
    if (app.globalData.storeInfo) {
      self.setData({
        store: app.globalData.storeInfo.name,
        storeId: app.globalData.storeInfo.id
      })
      self.getStoreProduct(app.globalData.storeInfo.id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})